package cn.zyy.hotel.controller.admin;

import cn.zyy.hotel.entity.Dept;
import cn.zyy.hotel.service.DeptService;
import cn.zyy.hotel.service.EmployeeService;
import cn.zyy.hotel.utils.Result;
import cn.zyy.hotel.utils.SystemConstant;
import cn.zyy.hotel.vo.DeptVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/dept")
public class DeptController {

    @Resource
    private DeptService deptService;
    @Resource
    private EmployeeService employeeService;

    //查询部门列表
    @RequestMapping("/list")
    public Result list(DeptVO deptVo){
        //设置分页
        PageHelper.startPage(deptVo.getPage(),deptVo.getLimit());
        //调用分页查询的方法
        List<Dept> deptList = deptService.selectDeptListByPage(deptVo);
        //创建分页对象
        PageInfo<Dept> pageInfo = new PageInfo<Dept>(deptList);
        //返回数据
        return Result.builder().code(0).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
    }

    //添加部门
    @RequiresPermissions("dept:add")
    @RequestMapping("/addDept")
    public Object addDept(Dept dept){
        Map<String, Object> map = new HashMap<String, Object>();
        //调用添加部门方法
        if (deptService.addDept(dept)>0){
            //成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else {
            //失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return map;
    }

    //修改部门
    @RequiresPermissions("dept:edit")
    @RequestMapping("/updateDept")
    public Object updateDept(Dept dept){
        Map<String, Object> map = new HashMap<String, Object>();
        //调用修改部门方法
        if (deptService.updateDept(dept)>0){
            //成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else {
            //失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return map;
    }

    //查询该部门下是否存在员工信息
    @RequestMapping("/checkDeptHasEmployee")
    public Object checkDeptHasEmployee(Integer id){
        Map<String, Object> map = new HashMap<String, Object>();
        //调用根据部门编号查询员工数量的方法
        if (employeeService.getEmployeeCountByDeptId(id)>0){
            //存在员工
            map.put(SystemConstant.EXIST,true);
            map.put(SystemConstant.MESSAGE,"该部门存在员工信息，无法删除");
        }else {
            //失败
            map.put(SystemConstant.EXIST,false);
        }
        return map;
    }

    //删除部门
    @RequiresPermissions("dept:delete")
    @RequestMapping("/deleteById")
    public Object deleteById(Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用删除部门的方法
        if(deptService.deleteById(id)>0){
            //成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else{
            //失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return map;
    }

    //查询所有部门
    @RequestMapping("/deptList")
    public Object deptList(){
        //调用查询所有部门信息的方法并返回到页面
        return deptService.selectDeptList();
    }


}
